package nio.udp;

import nio.tcp.server_client.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;

public class DatagramChannelReceiver {

    public static void main(String[] args) throws IOException, InterruptedException {

        try (DatagramChannel receiver = DatagramChannel.open()) {

            receiver.configureBlocking(false);
            receiver.bind(new InetSocketAddress("localhost", 2222));
//			receiver.connect(new InetSocketAddress("localhost", 1111));

            ByteBuffer bb = ByteBuffer.allocate(Server.buffer_size);
            while (true) {
                receiver.receive(bb); //receive和read一样
//				receiver.read(bb);
                bb.flip();
                System.out.println(Charset.forName("utf-8").newDecoder().decode(bb));
                bb.clear();
            }
        }
    }
}
